package com.naiterui.ehp.bs.b2c.order.service.impl;

import com.naiterui.ehp.bp.bo.b2c.order.ReturnOrderItemBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.b2c.order.entity.ReturnOrderItem;
import com.naiterui.ehp.bs.b2c.order.repository.IReturnOrderItemRepository;
import com.naiterui.ehp.bs.b2c.order.service.IReturnOrderItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description 退换货明细业务实现
 * @Author gongbaoqiang
 * @Date 2019/10/9 4:09 PM
 */
@Service
public class ReturnOrderItemServiceImpl implements IReturnOrderItemService {
    @Autowired
    private IReturnOrderItemRepository itemRepository;

    @Override
    public void addList(List<ReturnOrderItemBO> bos, String businessNo, String orderSn, String operator) throws BusinessException {
        List<ReturnOrderItem> list = new ArrayList<>();
        Optional.of(bos).ifPresent(it -> it.stream().forEach(s -> {
                    ReturnOrderItem item = new ReturnOrderItem();
                    BeanUtils.copyProperties(s, item);
                    item.setBusinessNo(businessNo);
                    item.setOrderSn(orderSn);
                    item.setCreatedAt(new Date());
                    item.setCreatedBy(operator);
                    list.add(item);
                }));
        itemRepository.save(list);

    }


    @Override
    public void delByBusinessNo(String businessNo) throws BusinessException {
        //保存明细---先删除后保存
        itemRepository.deleteByBusinessNo(businessNo);
    }
}
